package com.es.www.kyl.util;

import com.es.www.kyl.bean.UserContext;
import com.es.www.kyl.constant.BaseConstants;

import java.util.Objects;

/**
 * @Author by michangtao
 * @Date 2023/3/21 20:11
 * @Description 用户信息上下文
 */
public class UserContextHolder {

    private static final ThreadLocal<UserContext> USER_CONTEXT_HOLDER = ThreadLocal.withInitial(UserContext::new);

    public static void setUserContext(UserContext userContext) {
        USER_CONTEXT_HOLDER.set(userContext);
    }

    public static UserContext getUserContext() {
        return USER_CONTEXT_HOLDER.get();
    }

    public static void clearUserContext() {
        USER_CONTEXT_HOLDER.remove();
    }

    public static Integer getUserId() {
        return USER_CONTEXT_HOLDER.get().getUserId();
    }

    /**
     * 是否是超管
     */
    public static boolean isSuperAdmin() {
        return Objects.equals(USER_CONTEXT_HOLDER.get().getUserRole(), "0");
    }

    /**
     * 是否是会员以上权限
     */
    public static boolean isVipOrAdmin() {
        return Objects.equals(USER_CONTEXT_HOLDER.get().getUserRole(), BaseConstants.UserRole.VIP.getUserRole())
                || Objects.equals(USER_CONTEXT_HOLDER.get().getUserRole(), BaseConstants.UserRole.ADMIN.getUserRole());
    }

    /**
     * 是否有题库权限
     */
//    public static boolean hasQuestionPer() {
//        return Objects.equals(USER_CONTEXT_HOLDER.get().getPerType(), BaseConstants.PerType.QUESTION.getPerType())
//                || Objects.equals(USER_CONTEXT_HOLDER.get().getPerType(), BaseConstants.PerType.ALL.getPerType());
//    }
}
